package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.io.IOException;

public class LoginPage extends Utils {

    public static String url ="http://8.137.19.140:9090/blog_login.html";
    public LoginPage() {
        super(url);
    }


    //页面能够正常打开
    public void LoginRight() throws IOException {
        driver.findElement(By.cssSelector("body > div.container-login > div"));
    }

    //正常登录
    public void LogginSuc() throws InterruptedException {
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();

        //登陆后检查是否成功
        //方法一，检查页面元素
        driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title"));

        //方法二，用断言检查页面标题
        String title =   driver.getTitle();
        assert title.equals("博客列表页");


    }


    //异常登录
    public void LoginFail() throws InterruptedException {
        //因为登录成功后已经在列表页,点击注销
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //账号和密码为空
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(1000);

        Alert alert = driver.switchTo().alert();
        alert.accept();

        //账号和密码错误
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("12345");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(1000);

        alert.accept();
    }

}
